package ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import service.Impl.IBookServiceImpl;
import utils.WebUtils;
import vo.Book;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Date;

@WebServlet(name = "BookAddServlet")
public class BookAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

//        System.out.println(request.getParameter("name"));
//        System.out.println(request.getParameter("type"));
//        获取输入流
//        ServletInputStream inputStream = request.getInputStream();
//        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
//        StringBuilder stringBuilder=new StringBuilder();
//        String temp="";
//        while((temp=bufferedReader.readLine())!=null){
//            stringBuilder.append(temp);
//        }
//
//        //处理成json对象
//        String jsonString=stringBuilder.toString();
//        JSONObject jsonObject= JSON.parseObject(jsonString);
////        //解析json对象
//        Book book = new Book();
//
//        String name = jsonObject.getString("name");
//        String briefIntro = jsonObject.getString("briefIntro");
//        String author = jsonObject.getString("author");
//        String press = jsonObject.getString("press");
//        String imgUrl = jsonObject.getString("imgUrl");
//        int typeNum = jsonObject.getIntValue("type");
//        Date publicationDate = jsonObject.getSqlDate("publicationDate");
//        double price = jsonObject.getDouble("price");
//        int inventory = jsonObject.getIntValue("inventory");
//
        Book book = new Book();
//        book.setName("1");
////        book.setBriefIntro(briefIntro);
//        book.setAuthor("1");
//        book.setPress("1");
//        book.setImgUrl("1");
//        book.setType("1");
//        Date d = new Date(System.currentTimeMillis());
//        book.setPublication(d);
//        book.setPrice(1.1);
//        book.setInventory(1);

        String name = request.getParameter("name");
        String author = request.getParameter("author");
        String press = request.getParameter("press");
        String imgUrl = request.getParameter("imgUrl");
        int typeNum = Integer.parseInt(request.getParameter("type"));
        Date publicationDate = Date.valueOf(request.getParameter("publicationDate"));
        double price = Double.parseDouble(request.getParameter("price"));
        int inventory = Integer.parseInt(request.getParameter("inventory"));


        String type = "人文";
        switch (typeNum){
            case 0: break;
            case 1: type = "儿童";break;
            case 2: type = "教育";break;
            case 3: type = "文艺";break;
            default: break;
        }

        book.setName(name);
//        book.setBriefIntro(briefIntro);
        book.setAuthor(author);
        book.setPress(press);
        book.setImgUrl(imgUrl);
        book.setType(type);
        book.setPublication(publicationDate);
        book.setPrice(price);
        book.setInventory(inventory);

        IBookServiceImpl bookService = new IBookServiceImpl();
        bookService.addBook(book);

        System.out.println("1");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
